package cz.cvut.fel.taskpro.data.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

/**
 * This entity class represent user which is called Teacher, teacher have 
 * their templates and groups which teach.
 *
 * @author Tom
 * @version 1.0.0 beta
 */
@Entity
@Table(name = "kantor")
@DiscriminatorValue(value = "kantor")
@PrimaryKeyJoinColumn(name = "id")
@NamedQueries({
    @NamedQuery(name = "Kantor.findByZadani", query = "SELECT k FROM Kantor k WHERE k.mTemplates = :m_Zadani"),
    @NamedQuery(name = "Kantor.all", query = "SELECT k FROM Kantor k")
})
public class Kantor extends TaskProUser implements Serializable {

    private static final long serialVersionUID = 1L;
    @OneToMany(mappedBy = "kantor", fetch = FetchType.LAZY, cascade = {CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST})
    @OrderBy("dateOfCreation ASC")
    private List<Template> mTemplates;
    @OneToMany(mappedBy = "kantor", cascade = {CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST}, fetch = FetchType.LAZY)
    private List<GroupTaskPro> mGroups;

    public Kantor() {
    }

    public List<GroupTaskPro> getmGroups() {
        return mGroups;
    }

    public void setmGroups(List<GroupTaskPro> mGroups) {
        this.mGroups = mGroups;
    }

    public void addGroup(GroupTaskPro p) {
        if (this.mGroups == null) {
            mGroups = new ArrayList<GroupTaskPro>();
        }
        mGroups.add(p);
    }

    /**
     * prida dalsi zadani {@linkplain Template} od kantora
     *
     * @param newTemplate
     */
    public void addTemplate(Template newTemplate) {
        if (mTemplates == null) {
            mTemplates = new ArrayList<Template>();
        }
        mTemplates.add(newTemplate);

    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Kantor)) {
            return false;
        }
        Kantor other = (Kantor) object;
        if (getId() != other.getId()) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (int) getId();
        return hash;
    }

    @Override
    public String toString() {
        return "DL.entity.Kantor[ id=" + getId() + " login=" + getLogin() + " ]";
    }

    public List<Template> getmTemplates() {
        return mTemplates;
    }

    public void setmTemplates(List<Template> mTemplates) {
        this.mTemplates = mTemplates;
    }
}